package com.tedu.food.sys.service.impl;

import com.tedu.food.common.config.PageProperties;
import com.tedu.food.common.exception.ServiceException;
import com.tedu.food.common.util.ShiroUtils;
import com.tedu.food.common.vo.CategoryVo;
import com.tedu.food.common.vo.GoodsUserVo;
import com.tedu.food.common.vo.PageObject;
import com.tedu.food.sys.dao.CartItemDao;
import com.tedu.food.sys.dao.CategoryDao;
import com.tedu.food.sys.entity.CartItem;
import com.tedu.food.sys.entity.Category;
import com.tedu.food.sys.entity.Member;
import com.tedu.food.sys.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/***
 * Category(商品类目service接口实现类)
 * 
 * @author Daniel
 * @version 创建时间：2019年11月22日 下午3:01:41
 */
@Service
public class CategoryServiceImpl implements CategoryService {
	@Autowired
	private CategoryDao categoryDao;
	@Autowired
	private CartItemDao cartItemDao;
	@Autowired
	private PageProperties pageProperties;

	@Override
	public PageObject<Category> findCategoryObject(String categoryName, Integer pageCurrent) {
		if (pageCurrent == null || pageCurrent < 1)
			throw new IllegalArgumentException("当前页码值无效");
		int rowCount = categoryDao.getRowCount(categoryName);
		System.out.println(rowCount + ":" + categoryName + ":" + pageCurrent);
		if (rowCount == 0) {
			throw new IllegalArgumentException("系统没有查到对应记录");
		}
		int pageSize = pageProperties.getPageSize();
		int startIndex = (pageCurrent - 1) * pageSize;
		System.out.println("servce" + startIndex + " pageSize:" + pageSize);
		List<Category> categoryList = categoryDao.findCategoryObject(categoryName, startIndex, pageSize);
		return new PageObject<>(pageCurrent, pageSize, rowCount, categoryList);
	}

	@Override
	public int deleteCategoryObjectById(Integer... ids) {
		if (ids == null || ids.length == 0) {// 判断是否有ids传入
			throw new IllegalArgumentException("没有删除选项,请选择删除选项");
		}
		int rows;
		try {
			rows = categoryDao.deleteCategoryObjectById(ids);
		} catch (Throwable e) {
			e.printStackTrace();
			// 发出报警信息(例如给运维人员发短信)
			throw new ServiceException("系统故障，正在恢复中...");
		}
		if (rows == 0) {
			throw new ServiceException("记录可能已经不存在");
		}
		return rows;
	}

	@Override
	public int updateCategoryObject(Category category) {
		category.setModifiedTime(new Date());
		System.out.println("service-category:" + category);
		int row = categoryDao.updateCategoryObject(category);
		System.out.println("service层更新影响行数" + row);
		return row;
	}

	@Override
	public int insertCategoryObject(Category category) {
		category.setCreatedTime(new Date());
		int rows = categoryDao.insertCategoryObject(category);
		return rows;
	}

	@Override
	public Category findCategoryObjectById(Integer id) {
		return categoryDao.findCategoryObjectById(id);
	}

	@Override
	public List<CategoryVo> findAllCategoryGoods() {
		Member user = ShiroUtils.getUser();
		List<CategoryVo> categoryVo = categoryDao.findAllCategoryGoods();
		List<CartItem> cartItem = cartItemDao.findCartItemsBystatus(1, user.getId());// 状态为1的可用商品
		if (cartItem == null || cartItem.size() == 0)// 该用户购物车里无可用商品
			return categoryVo;
		List<Integer> goodsIds = new ArrayList<>();
		for (CartItem item : cartItem) {
			goodsIds.add(item.getGoodsId());
		}
		// 封装用户购物车商品数量
		for (CategoryVo cate : categoryVo) {
			for (GoodsUserVo goods : cate.getGoodsList()) {
				if (goodsIds.contains(goods.getId())) {
					for (CartItem it : cartItem) {
						if (it.getGoodsId() == goods.getId())
							goods.setUserNumber(it.getNumber());
					}
				}
			}
		}
		return categoryVo;
	}

	@Override
	public boolean categoryNameIsExists(String filedValue) {
		int falg = categoryDao.categoryNameIsExists(filedValue);
		if (falg == 1) {
			return true;
		} else {
			return false;
		}
	}

}
